const db = require('../mysql/db').promise()


class UserModel {
    // 根据id获取用户信息
    static async getUsers(id) {
        console.log(id)
        try {
            if (id) {
                const [rows] = await db.query('select * from users where id=?', [id])
                return rows
            }
            let [rows] = await db.query('select * from users')
            return rows
        } catch (err) {
            console.log('error==', err);

        }
    }

    static async addUsers({ user_id, user, user_name, password, age, birth, phone, position, address, entry_time }) {
        let lis=[user_id, user, user_name, password, age, birth, phone, position, address, entry_time]

        try {
            let [result] = await db.query('insert into users (user_id,user,user_name,password,age,birth,phone,position,address,entry_time) values (?,?,?,?,?,?,?,?,?,?)',lis)
            return result
        } catch (err) {
            console.log('error==', err);
        }

    }

    static async updateUsers(id, data) {
        try {
            const [result] = await db.query('update users set ? where id=?', [data, id])
            return result
        } catch (err) {
            console.log('error==', err);
        }
    }

    static async deleteUsers(id) {
        try {
            const [result] = await db.query('delete from users where id=?', [id]);
            return result;
        } catch (err) {
            console.log('error==', err);
        }
    }

}


module.exports = UserModel